import urllib.request
from bs4 import BeautifulSoup
chaper_url="https://pvp.qq.com/"#王者荣耀网站采用gbk
headers1 = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}#设置浏览器请求头
req = urllib.request.Request(url=chaper_url, headers=headers1)
response1 = urllib.request.urlopen(req)#打开整理后的url

response2=response1.read()#读取返回二进制是gbk编码的字节
#response3=response2.decode('gbk')#将gbk的编码译码为中文
soup = BeautifulSoup(response2,'html.parser',from_encoding='gbk')#直接传递b'***'给beautifulsoup

#soup2=soup.encode("gbk")
soup1=str(soup)#使用str()可将beautifulsoup的对象化为字符串。
with open('ddd.html','a+',encoding='utf8') as possFile:#将bs4之后的文本使用utf8编码格式写入文件
    possFile.write(soup1)
